//author:jxx
import options from "./turnstilesCarNumber.js";
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
let extension = {
    components: {//动态扩充组件或组件路径
        //表单header、content、footer对应位置扩充的组件
        gridHeader: '',//{ template: "<div>扩展组xx件</div>" },
        gridBody: '',
        gridFooter: '',
        //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
        modelHeader: '',
        modelBody: '',
        modelFooter: ''
    },
    buttons: { view: [], box: [], detail: [] },//扩展的按钮
    methods: {//事件扩展
        onInit() {
            this.boxOptions.labelWidth = 110
            this.searchFormFileds.createDate=[this.getStartDate() ,this.getEndDate()]
            //编辑
            this.columns.push({
                title: '操作',
                fixed: 'right',
                width: 140,
                render: (h, { row, column, index }) => {
                    return h(
                        "div", { style: {} }, [
                        h(
                            "a", {
                            props: {},
                            style: {},
                            on: {
                                click: (e) => {
                                    //this.$Message.info(row.accountsReceivable);
                                    // e.stopPropagation()
                                    //console.log(this.$refs.gridHeader)
                                    this.linkData(row);
                                }
                            }
                        },
                            "查看"
                        ),

                    ])
                }
            })
            //动态挂载自己加的MyComponent组件

            this.editFormFields.car_numbers = []; //["北京","天坛"]

            var car_numbers = this.getFormOption('car_numbers');
            car_numbers.type = "cascader";
            car_numbers.data = options.tree;
            car_numbers.formatter = nodeArr => {
                if (!nodeArr.length) {
                    return "";
                }
                return nodeArr[nodeArr.length - 1];
            };

            var effectiveStartDate = this.getFormOption('reserve_starttime');
            var effectiveEndDate = this.getFormOption('reserve_endtime');
            effectiveStartDate.onChange = (val, item) => {
                effectiveEndDate.min = val;
            }
            effectiveEndDate.onChange = (val, item) => {
                effectiveStartDate.max = val;
            }
            var mobile = this.getFormOption('mobile');
            mobile.onInput = (val) => {
                if (val.length == 11) {
                    this.http.post("api/Mall_turnstiles/GetPhone?mobile=" + val, '', false).then(reslut => {
                        if (!reslut.data.length) {
                            this.editFormFields.guest_name = reslut.data.guest_name;
                            this.editFormFields.ident = reslut.data.ident;
                            this.editFormFields.idcls = reslut.data.idcls;
                            options.tree.forEach(item => {
                                item.children.forEach(element => {
                                    if (element.label == reslut.data.car_number.slice(0, 2)) {
                                        this.editFormFields.car_numbers = [item.label, element.label]
                                    }
                                });
                            });
                            this.editFormFields.car_number = reslut.data.car_number.slice(2);
                        }
                    });
                }
            }
        },
        getStartDate (dates) {
          var dd = new Date();
          var n = dates || 0;
          dd.setDate(dd.getDate() + n);
          var y = dd.getFullYear();
          var m = dd.getMonth() + 1;
          var d = dd.getDate()-1;
          m = m < 10 ? "0" + m : m;
          d = d < 10 ? "0" + d : d;
          var day = y + "-" + m + "-" + d + " 00:00:00";
          return day;
      },
        getEndDate (dates) {
          var dd = new Date();
          var n = dates || 0;
          dd.setDate(dd.getDate() + n);
          var y = dd.getFullYear();
          var m = dd.getMonth() + 1;
          var d = dd.getDate()-1;
          m = m < 10 ? "0" + m : m;
          d = d < 10 ? "0" + d : d;
          var day = y + "-" + m + "-" + d + " 23:59:59";
          return day;
      },
               rowClick({ row, column, event }) { //查询界面table点击行选中当前行
                    this.$refs.table.$refs.table.toggleRowSelection(row);
                    return true;
                },

        getFormOption(field) {
            let option;
            this.editFormOptions.forEach(x => {
                x.forEach(item => {
                    if (item.field == field) {
                        option = item;
                    }
                })
            })
            return option;
        },
        modelOpenAfter() {
            let isEDIT = this.currentAction == this.const.EDIT;
            //点击弹出框后，如果是编辑状态，禁止编辑帐号，如果新建状态，将帐号字段设置为可编辑
            this.editFormOptions.forEach(item => {
                item.forEach(x => {
                    //如果是编辑帐号设置为只读
                    if (x.field == "guest_name") {
                        //disabled是editFormOptions的动态属性，这里只能通过this.$set修改值
                        this.$set(x, "disabled", isEDIT)
                    }
                    if (x.field == "mobile") {
                        //disabled是editFormOptions的动态属性，这里只能通过this.$set修改值
                        this.$set(x, "disabled", isEDIT)
                    }
                    if (x.field == "ident") {
                        this.$set(x, "disabled", isEDIT)
                    }
                    if (x.field == "idcls") {
                        this.$set(x, "disabled", isEDIT)
                    }
                    if (x.field == "reserve_starttime") {
                        this.$set(x, "disabled", isEDIT)
                    }
                    if (x.field == "reserve_endtime") {
                        this.$set(x, "disabled", isEDIT)
                    }
                    if (x.field == "car_number") {
                        this.$set(x, "disabled", isEDIT)
                    }
                })
            })
            if (this.currentAction == "update") {
                this.editFormFields.timeContainer = [row.reserve_starttime, row.reserve_endtime]
            }
        },
        addBefore(formData) { //新建保存前formData为对象，包括明细表
            //formData格式：
            // {
            //     mainData: { 主表字段1: 'x1', 主表字段2: 'x2' },
            //     detailData: [{ 明细表字段1: d1 }],
            //     delKeys: null //删除明细表行数据的id
            // }

            //formData.mainData.xxx="xxxx";//还可以继续手动添加值
            formData.mainData.car_number = formData.mainData.car_numbers.split(",")[1] + formData.mainData.car_number;
            //如果需要同时提交其他数据到后台，请设置formData.extra=xxxx
            //后台在表xxxxService.cs中重写Add方法即可从saveDataModel参数中拿到extra提交的对象
            //this.$Notice.success({ title: this.detailOptions.cnName + '新建前：', desc: '提前的数据：' + JSON.stringify(formData) });
            return this.veriFromTable(formData);
        },
        updateBefore(formData) { //编辑保存前formData为对象，包括明细表、删除行的Id
            return this.veriFromTable(formData)
        },
        // 保存前编辑 
        veriFromTable(formData) {
            let timeContainer = formData.mainData.timeContainer.split(",")
            if (timeContainer.length > 0 && timeContainer[0] != "") {
                formData.mainData.reserve_starttime = timeContainer[0]
                formData.mainData.reserve_endtime = timeContainer[1]
            } else {
                this.$Message.error("请选择有效起止日期！")
            }
            return true
        },

    }
};
export default extension;